package cn.libinal.scloud.base.entity;

import cn.libinal.scloud.base.common.entity.JpaBaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Sets;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.Set;

/**
 * @author libinal
 * created at 2018/11/4 15:20
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "sys_user")
public class User extends JpaBaseEntity {

    private static final long serialVersionUID = -2558522671545353163L;
    /**
     * 用户名
     */
    @Column(name="user_name", length = 100, unique = true, nullable = false)
    private String username;

    /**
     * 密码
     */
    @Column(name = "password", length = 100, nullable = false)
    @JsonIgnore
    private String password;

    /**
     * 手机号
     */
    @Column(name = "phone", length = 20, nullable = false)
    private String phone;

    /**
     * 邮箱
     */
    @Column(name = "email", length = 100)
    private String email;

    /**
     * 出生日期
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Column(name = "birthday")
    private LocalDate birthday;

    /**
     * 住址
     */
    @Column(name = "address", length = 200)
    private String address;

    /**
     * 是否可用
     */
    @Column(name = "is_enabled", length = 200)
    private Boolean enabled;

    @JsonIgnore
    @ManyToMany(cascade = {CascadeType.REFRESH}, fetch = FetchType.EAGER)
    @JoinTable(
            name = "sys_user_role",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
    )
    @OrderBy("priority")
    private Set<Role> roles = Sets.newLinkedHashSet();
}
